import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '@/views/HomeView.vue'
import { useUserStore } from '@/stores/user.js'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta: {
        title: '首页'
      }
    },
    {
      path: '/login',
      name: 'login',
      meta: {
        title: '登录'
      },
      component: () => import('@/views/LoginView.vue')
    },
    {
      path: '/user',
      name: 'user',
      meta: {
        title: '用户管理'
      },
      component: () => import('@/views/user/index.vue')
    },
    {
      path: '/challengeBox',
      name: 'challengeBox',
      meta: {
        title: '擂台'
      },
      component: () => import('@/views/challengeBox/index.vue')
    },
    {
      path: '/challboxblack',
      name: 'challboxblack',
      meta: {
        title: '擂台黑名单'
      },
      component: () => import('@/views/white/challboxblack.vue')
    },

    {
      path: '/challboxwhite',
      name: 'challboxwhite',
      meta: {
        title: '擂台白名单'
      },
      component: () => import('@/views/white/challboxwhite.vue')
    },
    {
      path: '/white',
      name: 'white',
      meta: {
        title: '白名单'
      },
      component: () => import('@/views/white/index.vue')
    },
    {
      path: '/wxsBlack',
      name: 'wxsBlack',
      meta: {
        title: '无限黑'
      },
      component: () => import('@/views/wxsBlack/black.vue')
    },
    {
      path: '/tmr',
      name: 'tmr',
      meta: {
        title: '天命人'
      },
      component: () => import('@/views/tmr/index.vue')
    },
    {
      path: '/chest',
      name: 'chest',
      meta: {
        title: '用户宝箱/福袋'
      },
      component: () => import('@/views/chest/chest.vue')
    },
    {
      path: '/welfare',
      name: 'welfare',
      meta: {
        title: '福利屋管理'
      },
      component: () => import('@/views/welfare/index.vue')
    },
    {
      path: '/packet',
      name: 'packet',
      meta: {
        title: '红包管理'
      },
      component: () => import('@/views/packet/index.vue')
    },
    {
      path: '/recharge',
      name: 'recharge',
      component: () => import('@/views/recharge/index.vue'),
      meta: {
        title: '充值记录'
      }
    },
    {
      path: '/goodorder',
      name: 'goodorder',
      component: () => import('@/views/goodorder/goodorder.vue'),
      meta: {
        title: '商城订单管理'
      }
    },
    {
      path: '/adverOrder',
      name: 'adverOrder',
      component: () => import('@/views/goodorder/adverOrder.vue'),
      meta: {
        title: '进阶订单管理'
      }
    },
    {
      path: '/profit',
      name: 'profit',
      component: () => import('@/views/profit/index.vue'),
      meta: {
        title: '盈亏数据'
      }
    },
    {
      path: '/luckyhouse',
      name: 'luckyhouse',
      component: () => import('@/views/luckyhouse/index.vue'),
      meta: {
        title: '幸运屋'
      }
    },
    {
      path: '/luckysjm',
      name: 'luckysjm',
      component: () => import('@/views/luckyhouse/luckysjm.vue'),
      meta: {
        title: '幸运屋随机码'
      }
    },
    {
      path: '/draw',
      name: 'draw',
      component: () => import('@/views/draw/index.vue'),
      meta: {
        title: '开盒记录'
      }
    },
    {
      path: '/give',
      name: 'give',
      component: () => import('@/views/give/index.vue'),
      meta: {
        title: '赠礼记录'
      }
    },
    {
      path: '/promotion',
      name: 'promotion',
      component: () => import('@/views/promotion/index.vue'),
      meta: {
        title: '推广员申请'
      }
    },
    {
      path: '/bag',
      name: 'bag',
      component: () => import('@/views/bag/index.vue'),
      meta: {
        title: '用户擂台袋赏'
      }
    },
    {
      path: '/operationlog',
      name: 'operationlog',
      component: () => import('@/views/operationlog/operationlog.vue'),
      meta: {
        title: '充值/扣除操作日志'
      }
    },
    {
      path: '/give2',
      name: 'give2',
      component: () => import('@/views/challengeBox/give.vue'),
      meta: {
        title: '擂台赠礼记录'
      }
    }
  ]
})

router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  if (to.name !== 'login' && !userStore.username) {
    return next('/login')
  }
  document.title = `${to.meta.title || '后台管理'}` // 设置页面标题
  next()
})

export default router
